#include<iostream>
#include<vector>
#include<cctype>

using namespace std;

typedef enum type
{
	op,
	num
}NodeType;

class Node
{
	public:
		virtual ~Node()
		{
			vector<Node*>::iterator it = child.begin();
			while(it != child.end())
			{
				delete *it;
				it++;
			}
		}
		vector<Node*> child;
		NodeType type;
};

double T();
double E1(double);
double T1(double);
double F();
double N();

char peek;


void lookahead()
{
	cin>>peek;
}

double E()
{
	double left = T();
	cout<<"E:left is:"<<left<<endl;
	return E1(left);
}

double T()
{
	double left = F();
	cout<<"T:left is:"<<left<<endl;
	return T1(left);
}

double E1(double left)
{
	double right;
	double result;
	switch(peek)
	{
		case'+':
			lookahead();
			right = T();
			cout<<"+ left right "<<left<<"------"<<right<<endl;
			result = left+right;
			return E1(result);
			break;
		case'-':lookahead();
			right = T();
			cout<<"- left right "<<left<<"------"<<right<<endl;
			result = left-right;
			return E1(result);
			break;
		default:
			return left;
			break;
	}
}

double T1(double left)
{
	double right;
	double result;
	switch(peek)
	{
		case'*':
			lookahead();
			right = F();
			cout<<"* left right "<<left<<"------"<<right<<endl;
			result = left*right;
			return T1(result);
			break;
		case'/':
			lookahead();
			right = F();
			cout<<"/ left right "<<left<<"------"<<right<<endl;
			result = left/right;
			return T1(result);
			break;
		case'%':
			lookahead();
			right = F();
			cout<<"% left right "<<left<<"-----"<<right<<endl;
			result = (int)left%(int)right;
			return T1(result);
			break;
		default:
			return left;
			break;
	}
}

double F()
{
	double result;
	if(peek == '(')
	{
		lookahead();
		result = E();
		if(peek == ')')
		{
			lookahead();
			return result;
		}else
		{
			cout<<") error"<<endl;
		}
	}else if(isdigit(peek))
	{
		result = N();
		return result;
	}else
	{
		cout<<"error"<<endl;
	}
}

double N()
{
	int cv = (int)(peek - 48);
	do{
		lookahead();
		if(isdigit(peek))
		{
			int aa = (int)(peek - 48);
			cv = cv*10+aa;
		}else
		{
			break;
		}
	}while(true);
	
	cout<<"cv is:"<<cv<<endl;
	return cv;
}

int main(int argc,char* args[])
{
	lookahead();
	double result = E();
	cout<<"the value of E:"<<result<<endl;
	return 0;
}
